﻿using System;
using System.Windows.Forms;
using Business;
using Business.Entity.Contabil;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Entity.Sped;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Contabil
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FormTipoLancamento : FormDefault, IForm
    {
        /// <summary>
        /// 
        /// </summary>
        public FormTipoLancamento()
        {
            InitializeComponent();

            // Configuração dos botões de comando do formulário.
            cmdCancelar.Form = this;
            cmdExcluir.Form = this;
            cmdSalvar.Form = this;

            PlanoContaReferencialRepository.Session = TipoLancamentoRepository.GetSession();
            contraPartidaAcrescimosBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            contraPartidaDescontoBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            contraPartidaJurosBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            contraPartidaValorBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            partidaValorBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            partidaJurosBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            partidaDescontoBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            partidaAcrescimosBindingSource.DataSource = PlanoContaReferencialRepository.GetList();
            InitFocusControl = descricaoTextEdit;
        }

        public object Novo()
        {
            var tl = new TipoLancamento();
            CurrentObject = tl;
            descricaoTextEdit.Focus();
            ShowDialog();
            if (tl.Id == 0)
            {
                return null;
            }
            return tl;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            descricaoTextEdit.Focus();
            ShowDialog();
        }


        public object Save()
        {
            if (!Validate().HasError)
            {
                TipoLancamentoRepository.Save((TipoLancamento)CurrentObject);
            }

            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            TipoLancamentoRepository.Delete((TipoLancamento)entity);
            return true;
        }


        public object CurrentObject
        {
            get
            {
                tipoLancamentoBindingSource.EndEdit();
                return tipoLancamentoBindingSource.DataSource;
            }
            set
            {
                tipoLancamentoBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            return DataValidation.ValidateEntity((TipoLancamento)CurrentObject);
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}